$(function(){
	plugins_init_function.push(menu_init);
	plugins_config_get_function.push(bulid_menu);
    $('#menuModal').on('show.zui.modal', function (e) {
        chooseMediaResCallBack = changeMenuImg;
    });
   
})
function menu_init(){
	initmenuCustom(panoConfig.custom_menu);
}
function openMenu() {
    $("#menuModal").modal('show');
}
function initmenuCustom(data) {
    // console.log(data);
    $(data.menuSettings).each(function (idx) { 
        addMenu();
        $("#menuModal .modal-body .menu-row:last img").attr('src', this.imgPath);
        $("#menuModal .modal-body .menu-row:last input:eq(1)").val(this.content);
        $("#menuModal .modal-body .menu-row:last input:first").val(this.title);
    });
}
function addMenu() {
    if (hasThreeRowMenu()) {
        $.zui.messager.show('可添加数量已达上限', {placement: 'center', type: 'warning', time: '3000', icon: 'warning-sign'});
        return;
    }
    var menuStr = '<div class="row menu-row" style="margin:5px auto;">' +
        '<div class="col-md-3 clearfix">' +
        '<img src="/static/images/skin1/iconfont-link.png">' +
        '<div class="img-right-a">' +
        '<a data-modalid="#media_icon" data-imgtype="system" data-subtype="2" href="javascript:void(0);">系统图标</a>' +
        '<a data-modalid="#media_icon" data-imgtype="custom" href="javascript:void(0);">媒体库图标</a>' +
        '</div>' +
        '</div>' +
        '<div class="col-md-3">' +
        '<input type="text" class="form-control" placeholder="输入名称">' +
        '</div>' +
        '<div class="col-md-4">' +
        '<input type="text" class="form-control" placeholder="输入跳转地址">' +
        '</div>' +
        '<div class="col-md-2">' +
        '<button class="btn" onclick="deleteMenuRow(this)">删除</button>' +
        '</div>' +
        '</div>';
    $("#menuModal .modal-body").append(menuStr);
}
function hasThreeRowMenu() {
    return $("#menuModal .menu-row").length == 3;
}

function menuOkClick() {
    var flag = true;
    $("#menuModal .menu-row").each(function (idx) {
        var msg = '';
        $(this).find("input").each(function (idx) {
            
            if (!$(this).val()) {
                msg = '请填写必要信息';
                flag = false;
                return false;
            }
            
        });
        if (!flag) {
            $.zui.messager.show(msg, {placement: 'center', type: 'warning', time: '3000', icon: 'warning-sign'});
            return false;
        }
    });
    if (flag) {
        $("#menuModal").modal("hide");
    }
}

function deleteMenuRow(el) {
    $(el).parent().parent().remove();
}

function changeMenuImg(data) {
    $(openMediaResObj).parent().prev().attr("src", data.src);
}


function bulid_menu(panoConfig){
	var menuJson = {};
	menuJson.menuSettings = [];
	if ($("#menuModal .modal-body .menu-row").length > 0) {
	    $("#menuModal .modal-body .menu-row").each(function (idx) {
	        var menuObj = {};
            menuObj.imgPath = $(this).find("img").attr("src");
	        menuObj.title = $(this).find("input:eq(0)").val();
            var url = $(this).find("input:eq(1)").val();
            menuObj.content = url;
	        menuJson.menuSettings.push(menuObj);
	    });
	}
	panoConfig.custom_menu = menuJson;
}